<template>
  <div class="createMedicalAdvice-container">
    <div class="top-title-container">
      <modules-title :title-name="title">
        <template slot="btn">
          <el-button
            class="main-btn"
            size="small"
            type="primary"
            @click="submit"
            v-if="detail!=1"
          >保存</el-button
          >
          <el-button
            class="main-btn"
            size="small"
            type="primary"
            @click="changeEdit"
            v-if="detail==1"
          >编辑</el-button
          >
          <el-button class="border-btn" size="small" @click="cancelOption"
          >取消</el-button
          >
<!--          <el-button-->
<!--            class="main-btn"-->
<!--            size="small"-->
<!--            type="primary"-->
<!--            @click="submit"-->
<!--            >保存</el-button-->
<!--          >-->
<!--          <el-button class="border-btn" size="small" @click="cancelOption"-->
<!--            >取消</el-button-->
<!--          >-->
        </template>
      </modules-title>
    </div>
    <div class="createMedicalAdvice-content">
      <el-form ref="form" :model="form" label-width="80px" :disabled="detail==1">
        <el-form-item label="医嘱名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="医嘱内容">
          <el-input
            type="textarea"
            v-model="form.contents"
            maxlength="120"
            show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { medicalAdviceCreate, medicalAdviceEdit, medicalAdviceDetail } from "@/api/templates.js";
export default {
  name: "createMedicalAdvice",
  data() {
    return {
      editId: 0,
      form: {
        title: "",
        contents: "",
      },
      detail:null,
      title:''
    };
  },
  created(){
    this.editId = Number(this.$route.query.id)?Number(this.$route.query.id): 0
    this.detail = this.$route.query.detail
    if(this.detail==1){
      this.title = '详情'
    }else if(this.editId >0){
      this.title = '编辑医嘱'
    }else{
      this.title = '新建医嘱'
    }
    if(this.editId){
      this.getPrescriptionDetails(this.editId)
    }
  },
  methods: {
    changeEdit(){
      this.detail=null
      this.title='编辑医嘱'
    },
    cancelOption() {
      this.$router.push({
        name: "medicalAdvice",
      });
    },
    submit() {
      if (this.editId) {
        this.submitMedicalAdviceEdit();
      } else {
        this.submitMedicalAdviceCreate();
      }
    },
    submitMedicalAdviceCreate() {
      medicalAdviceCreate(this.form).then((res) => {
        console.log(res);
        this.$router.push({
          name: "medicalAdvice",
        });
      });
    },
    getPrescriptionDetails(id) {
      medicalAdviceDetail(id).then((res) => {
        if (res.code == 200) {
          this.form = res.data;
        }
      });
    },
    submitMedicalAdviceEdit() {
      let edit = {
        id: this.form.ID,
        title: this.form.title,
        contents: this.form.contents
      };
      medicalAdviceEdit(edit).then((res) => {
        if (res.code == 200) {
          console.log("修改成功");
          this.$router.push({
            name: "medicalAdvice",
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/scss/templates/createMedicalAdvice/createMedicalAdvice.scss";
</style>
